{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4efe6c25",
   "metadata": {},
   "source": [
    "## 元祖"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41108e17",
   "metadata": {},
   "source": [
    "### 1.作用\n",
    "元组与列表类似，也是可以存多个任意类型的元素，不同之处在于元组的元素不能修改，即元组相当于不可变的列表，用于记录多个固定不允许修改的值，单纯用于取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0fec269",
   "metadata": {},
   "source": [
    "### 2.定义方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5cd03eea",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-2-bba0a20fed26>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-2-bba0a20fed26>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    countries = (\"中国\"，\"美国\"，\"英国\")  # 本质:countries = tuple(\"中国\"，\"美国\"，\"英国\")\u001b[0m\n\u001b[0m                     ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "# 在()内用逗号分隔开多个任意类型的值\n",
    ">>> countries = (\"中国\"，\"美国\"，\"英国\")  # 本质:countries = tuple(\"中国\"，\"美国\"，\"英国\")\n",
    "# 强调：如果元组内只有一个值，则必须加一个逗号，否则()就只是包含的意思而非定义元组\n",
    ">>> countries = (\"中国\"，)  # 本质:countries = tuple(\"中国\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fc11e51",
   "metadata": {},
   "source": [
    "### 3.类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbc23be2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 但凡能被for循环的遍历的数据类型都可以传给tuple()转换成元组类型\n",
    ">>> tuple('wdad') # 结果：('w', 'd', 'a', 'd') \n",
    ">>> tuple([1,2,3]) # 结果：(1, 2, 3)\n",
    ">>> tuple({\"name\":\"jason\",\"age\":18}) # 结果：('name', 'age')\n",
    ">>> tuple((1,2,3)) # 结果：(1, 2, 3)\n",
    ">>> tuple({1,2,3,4}) # 结果：(1, 2, 3, 4)\n",
    "# tuple()会跟for循环一样遍历出数据类型中包含的每一个元素然后放到元组中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d35bc3d",
   "metadata": {},
   "source": [
    "### 4.使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eaa7236d",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-40e068e58848>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;36m22\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'hehe'\u001b[0m  \u001b[0;31m# 报错：TypeError:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0;31m# 2、切片(顾头不顾尾，步长)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    ">>> tuple1 = (1, 'hhaha', 15000.00, 11, 22, 33) \n",
    "# 1、按索引取值(正向取+反向取)：只能取，不能改否则报错！  \n",
    ">>> tuple1[0]\n",
    "1\n",
    ">>> tuple1[-2]\n",
    "22\n",
    ">>> tuple1[0] = 'hehe'  # 报错：TypeError:\n",
    "\n",
    "# 2、切片(顾头不顾尾，步长)\n",
    ">>> tuple1[0:6:2] \n",
    "(1, 15000.0, 22)\n",
    "\n",
    "# 3、长度\n",
    ">>> len(tuple1)  \n",
    "6\n",
    "\n",
    "# 4、成员运算 in 和 not in\n",
    ">>> 'hhaha' in tuple1 \n",
    "True\n",
    ">>> 'hhaha' not in tuple1  \n",
    "False \n",
    "\n",
    "# 5、循环\n",
    ">>> for line in tuple1:\n",
    "...     print(line)\n",
    "1\n",
    "hhaha\n",
    "15000.0\n",
    "11\n",
    "22\n",
    "33"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df9c7133",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
